package com.woniu.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.UrlPermission;
import com.woniu.model.Result;
import com.woniu.service.UrlPermissionService;
import com.woniu.service.util.UserTokenUtil;
import com.woniu.service.vo.PermissionShow;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 菜单和接口权限定义 前端控制器
 * </p>
 *
 * @author D
 * @since 2023-03-27 08:24:57
 */
@RestController
@RequestMapping("/urlPermission")
public class UrlPermissionController {

    @Resource
    UrlPermissionService urlPermissionService;

    /**
     * 根据id查询权限
     * @param request 用于获取用户id
     * @return 权限列表
     */
    @RequestMapping("/selectListById")
    public Result<List<PermissionShow>> selectListById(HttpServletRequest request) {
        Long userCode = Long.valueOf(UserTokenUtil.getUserAccount(request));
        List<PermissionShow> showList = urlPermissionService.selectListById(userCode);
        return Result.successResult(showList);
    }

    /**
     * 查询所有菜单权限
     *
     * @return
     */
    @RequestMapping("/selectList")
    public Result<List<PermissionShow>> selectList() {
        List<PermissionShow> showList = urlPermissionService.selectList();
        return Result.successResult(showList);
    }

    /**
     * 根据角色id查询绑定的权限
     *
     * @param roleId 角色id
     * @return 权限id数组
     */
    @RequestMapping("/getPermissionIds")
    public Result<Set<Long>> getPermissionIds(Long roleId) {
        Set<Long> permissionIds = urlPermissionService.getPermissionIds(roleId);
        return Result.successResult(permissionIds);
    }


    /**
     * 给角色配置权限
     *
     * @param roleId 角色id
     * @param permissionIds 权限id数组
     * @return ok
     */
    @RequestMapping("/addAuthority")
    public Result<String> addAuthority(Long roleId, @RequestParam(value = "permissionIds[]", required = false) Long[] permissionIds) {
        if (permissionIds == null) {
            permissionIds = new Long[0];
        }
        urlPermissionService.addAuthority(roleId, permissionIds);
        return Result.successResult("ok");
    }

    /**
     * 分页查询所有菜单
     *
     * @param currentPage 当前页
     * @param pageSize   每页显示条数
     * @param keyword   关键字
     * @return  分页结果
     */
    @RequestMapping("/pageList")
    public Result<Page<UrlPermission>> pageList(Integer currentPage, Integer pageSize, String keyword) {
        Page<UrlPermission> urlPermissionPage = urlPermissionService.pageList(currentPage, pageSize, keyword);
        return Result.successResult(urlPermissionPage);

    }

    /**
     * 删除权限
     *
     * @param permissionId 权限id
     * @return ok
     */
    @RequestMapping("/deleteById/{permissionId}")
    public Result<String> deleteById(@PathVariable("permissionId") Long permissionId) {
        urlPermissionService.deleteById(permissionId);
        return Result.successResult("ok");
    }

    /**
     * 添加权限
     *
     * @param urlPermission 权限信息
     * @return ok
     */
    @RequestMapping("/addPermission")
    public Result<String> addPermission(UrlPermission urlPermission) {
        urlPermission.setInsertTime(new Date()).setUpdateTime(new Date());
        urlPermissionService.save(urlPermission);
        return Result.successResult("ok");
    }

    /**
     * 修改权限菜单信息
     * @param urlPermission 权限信息
     * @return ok
     */
    @RequestMapping("/updatePermission")
    public Result<String> updatePermission(UrlPermission urlPermission) {
        urlPermission.setUpdateTime(new Date());

        urlPermissionService.updateById(urlPermission);
        return Result.successResult("ok");
    }
}

